#include "workflow.h"

namespace acidone
{

	WorkflowBase::WorkflowBase(CategoryManager *categoryManager)
		:m_categoryManager(categoryManager)
	{
	}

	CategoryManager* WorkflowBase::categoryManager() const
	{
		return m_categoryManager;
	}

	Category* WorkflowBase::startCategory() const
	{
		return m_categoryManager->embeddedCategory(CategoryManager::In);
	}

	Category* WorkflowBase::endCategory() const
	{
		return m_categoryManager->embeddedCategory(CategoryManager::Done);
	}

	ManualWorkflow::ManualWorkflow(CategoryManager *categoryManager)
		:WorkflowBase(categoryManager)
	{
	}

	bool ManualWorkflow::canBeDone(Task *task) const
	{
		Q_UNUSED(task);

		return true;
	}

	Database::Categories ManualWorkflow::nextCategories(Category *category) const
	{
		Q_ASSERT(m_categoryManager->isEmbeddedCategory(category));
		Q_UNUSED(category);

		Database::Categories categories = m_categoryManager->embeddedCategories();
		categories.removeOne(endCategory());

		return categories;
	}

	AcidWorkflow::AcidWorkflow(CategoryManager *categoryManager)
		:WorkflowBase(categoryManager)
	{
	}

	bool AcidWorkflow::canBeDone(Task *task) const
	{
		return m_categoryManager->embeddedCategory(CategoryManager::Day) ==
				m_categoryManager->getTaskEmbeddedCategory(task);
	}

	Database::Categories AcidWorkflow::nextCategories(Category *category) const
	{
		Q_ASSERT(m_categoryManager->isEmbeddedCategory(category));

		CategoryManager::EmbeddedCategory type = m_categoryManager->embeddedCategoryType(category);
		Database::Categories categories;

		switch(type)
		{
		case CategoryManager::In:
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::In));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Day));
			break;
		case CategoryManager::Day:
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Day));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Later));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Control));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Maybe));
			break;
		case CategoryManager::Later:
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Day));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Later));
			break;
		case CategoryManager::Control:
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Day));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Control));
			break;
		case CategoryManager::Maybe:
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Day));
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Maybe));
			break;
		case CategoryManager::Done:
			categories.append(m_categoryManager->embeddedCategory(CategoryManager::Done));
			break;
		default:
			Q_ASSERT(false);
			break;
		}

		return categories;
	}
}
